//--------------插件
var gulp = require("gulp");//请求gulp
var htmlclean = require("gulp-htmlclean");//压缩html
var imagemin = require("gulp-imagemin");//压缩图片
var uglify = require("gulp-uglify");//压缩js
var stripdebug = require("gulp-strip-debug");//清除测试代码
var concat = require("gulp-concat");//合并js
var less = require("gulp-less");//将less转化css
var postcss = require("gulp-postcss");//可执行添加前缀
var autoprefixer = require("autoprefixer");//添加前缀
var cssnano = require("cssnano");//压缩css代码
var connect = require("gulp-connect");//压缩css代码


//环境
var devMode = process.env.NODE_ENV == "development";
console.log(devMode);
// gulp.src()//读文件
// gule.dest()//写入的文件地址
// gulp.task()//创建任务
// gulp.watch()//监听


var folder = {
	src : "src/",//开发目录文件夹
	dist : "dist/"//压缩打包后目录文件夹
}

gulp.task("html",function(){
	var page = gulp.src(folder.src + "html/*")
		.pipe(connect.reload())
	if(!devMode){
		page.pipe(htmlclean())
	}
		page.pipe(gulp.dest(folder.dist + "html/"))

})

gulp.task("images",function(){
	gulp.src(folder.src + "images/*")
		.pipe(imagemin())
		.pipe(gulp.dest(folder.dist + "images/"))

})

gulp.task("js",function(){
	var page = gulp.src(folder.src + "js/*")
		.pipe(connect.reload())
	if(!devMode){
		page.pipe(stripdebug())
		// .pipe(concat("main.js"))
		.pipe(uglify())
	}
		
		page.pipe(gulp.dest(folder.dist + "js/"))

})

gulp.task("css",function(){
	var options = [autoprefixer(),cssnano()];
	var page = gulp.src(folder.src + "css/*")
		.pipe(less())
		.pipe(connect.reload())
	if(!devMode){
		page.pipe(postcss(options))
	}
		page.pipe(gulp.dest(folder.dist + "css/"))

})

gulp.task("watch",function(){
	gulp.watch(folder.src + "html/*",["html"]);
	gulp.watch(folder.src + "css/*",["css"]);
	gulp.watch(folder.src + "js/*",["js"]);
	gulp.watch(folder.src + "images/*",["images"]);

})

gulp.task("server",function(){
	connect.server({
		port : "8090",
		livereload : true
	})
})
gulp.task("default",["html","images","js","css","watch","server"])